#coding=utf-8
'''
Created on 2017-07-14

@author: sandy
'''

from twisted.internet import task
import logging
from pycommon import Const,DbPool,Utils
from pycommon import Pack as packet



class GameDB(object):
    def __init__(self):
        self.__linkcount = 0
        self.__dbp = None

    def start(self,db,user,passwd,host=u'127.0.0.1',port=3306,linkcount=3):
        logging.info("gamedb host=%s db=%s user=%s",host,db,user)
        l = task.LoopingCall(self.onTimer)
        l.start(10, False)
        self.__linkcount = linkcount
        self.__dbp = DbPool.DBConnectionPool(db = db,user = user,passwd = passwd,host = host,port = port,linkcount = linkcount)
        self.__dbp.start()

    def stop(self):
        self.__dbp.stop()
        
    def onTimer(self):
        self.doTask()

    
    def doTask(self):
        pass
            
    def hashIndex(self, uid):
        return uid % self.__linkcount
    
    def hashIndexAccount(self, account):
        return ord(account[0]) % self.__linkcount  
    
    def query(self, conindex, ctx, sql, params, func):
        self.__dbp.query(conindex, ctx, sql, params, func)

    def execute(self, conindex, ctx, sql, params, func):
        self.__dbp.execute(conindex, ctx, sql, params, func)

    def call(self, conindex, ctx, proc, params, func):
        self.__dbp.call(conindex, ctx, proc, params, func)

    def payResult(self):
        pass

    def paySucess(self,orderId):
        logging.debug("orderId=%d paySucess",orderId)
        sql = u"call p_order_notify_recived(%s)"
        params = (orderId,)
        ctx = None
        self.query(self.hashIndex(self.lastFinishOrderId),ctx,sql,params,self.paySucessResult)
        pass

    def paySucessResult():
        pass
    

instance = GameDB()

if __name__ == '__main__':
    pass